//
//  BaseView.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/10/20.
//

import Cocoa

class BaseView: NSView {
    
    var mouseDownStyle:ViewStyle?
    var mouseExitStyle:ViewStyle?
    var mouseEnteredStyle:ViewStyle?
    var defaultStyle:ViewStyle?
    /**
     鼠标点击时执行
     */
    var mouseDownHandler: (() -> Void)?
    /**
     鼠标移出时，执行函数
     */
    var mouseExitHandler: (() -> Void)?
    var singleButton:Bool = false
    var selected:Bool = false
    var defaultSelect:Bool = false
    var click:SimpleClickHandler?
    var onlyId:String?
    var baseLabel:DULabel?
    var isEnabled:Bool = false
    
    override func layout() {
        setStyle(defaultStyle)
        if defaultSelect {
            setStyle(mouseDownStyle)
        }
    }
    
    override func mouseDown(with event: NSEvent) {
        if isEnabled {
            return
        }
        if selected {
            return
        }
        self.setStyle(mouseDownStyle)
        click?.onClick(self)
        mouseDownHandler?()
    }
    
    override func mouseExited(with event: NSEvent) {
        if isEnabled {
            return
        }
        if singleButton && selected {
            return
        }

        mouseExitHandler?()
        mouseExitStyle = mouseExitStyle == nil ? defaultStyle : mouseExitStyle
        self.setStyle(mouseExitStyle)
    }
    
    override func mouseEntered(with event: NSEvent) {
        if isEnabled {
            return
        }
        if singleButton && selected {
            return
        }
        self.setStyle(mouseEnteredStyle)
    }
    
    func setStyle(_ style:ViewStyle?) {
        self.layer?.backgroundColor = style?.backgroundColor
        self.layer?.borderWidth = style == nil ? 0 : style!.borderWidth
        self.layer?.cornerRadius = style == nil ? 0 : style!.radius
        self.layer?.borderColor = style?.borderColor
        self.baseLabel?.textColor = style?.textColor
    }
    
    //NSView发生改变时，重新调整监听区域及事件
    var trackingArea: NSTrackingArea?
    
    func installTrackingArea() {
        self.window?.acceptsMouseMovedEvents = true
        //self.window?.ignoresMouseEvents = true
        if trackingArea != nil {
            self.removeTrackingArea(trackingArea!)
        }
        
        let trackingOptions:NSTrackingArea.Options = [.activeAlways,.mouseEnteredAndExited,.mouseMoved]
        trackingArea = NSTrackingArea(rect: self.bounds, options: trackingOptions, owner: self, userInfo: nil)
        self.addTrackingArea(trackingArea!)
    }
    
    override func viewDidMoveToSuperview() {
        super.viewDidMoveToSuperview()
        installTrackingArea()
    }
    
    override func updateTrackingAreas() {
        super.updateTrackingAreas()
        installTrackingArea()
    }
    
    struct ViewStyle {
        var backgroundColor:CGColor?
        var borderColor:CGColor?
        var radius:CGFloat = 8
        var borderWidth:CGFloat = 0
        var textColor:NSColor?
    }
    
    class ViewStyleUtil {
        static func dayDefaultStyle() -> ViewStyle {
            var s = ViewStyle()
            s.backgroundColor = nil
            s.borderColor = nil
            s.radius = 8
            s.borderWidth = 0
            return s
        }
        static func dayCurrentStyle() -> ViewStyle {
            var s = ViewStyle()
            s.backgroundColor = CGColor(red: 0.96, green: 0.52, blue: 0.52, alpha: 0.6)
            s.borderColor = CGColor(red: 0.96, green: 0.52, blue: 0.52, alpha: 1)
            s.radius = 8
            s.borderWidth = 2
            return s
        }
        static func dayCurrentExitStyle() -> ViewStyle {
            var s = ViewStyle()
            s.backgroundColor = CGColor(red: 0.96, green: 0.52, blue: 0.52, alpha: 0.3)
            s.borderColor = CGColor(red: 0.96, green: 0.52, blue: 0.52, alpha: 0.5)
            s.radius = 8
            s.borderWidth = 2
            return s
        }
        
        
        static func dayDownStyle() -> ViewStyle {
            var s = ViewStyle()
            s.backgroundColor = nil
            s.borderColor = CGColor(red: 0.3, green: 0.43, blue: 0.95, alpha: 0.9)
            s.radius = 8
            s.borderWidth = 2
            return s
        }
        static func dayEnteredStyle() -> ViewStyle {
            var s = ViewStyle()
            s.backgroundColor = nil
            s.borderColor = CGColor(red: 0.3, green: 0.43, blue: 0.95, alpha: 0.5)
            s.radius = 8
            s.borderWidth = 2
            return s
        }
        
        static func notCurrentMonthDownStyle () -> ViewStyle {
            var s = ViewStyle()
            s.backgroundColor = nil
            s.borderColor = CGColor(red: 0.56, green: 0.56, blue: 0.56, alpha: 0.8)
            s.borderWidth = 2
            s.radius = 8
            return s
        }
        
        static func notCurrentMonthEnteredStyle () -> ViewStyle {
            var s = ViewStyle()
            s.backgroundColor = nil
            s.borderColor = CGColor(red: 0.56, green: 0.56, blue: 0.56, alpha: 0.5)
            s.borderWidth = 2
            s.radius = 8
            return s
        }
    }
}
